package workquene;

import com.rabbitmq.client.*;
import utils.RabbitMQUtils;

import java.io.IOException;

/**
 * 能者多劳模式
 * 1.消费者手动确认消息
 * 2.一次消费一个消息
 */

public class Customer2 {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare("work", true, false, false, null);

        // 每次只消费一个消息
        channel.basicQos(1);


        channel.basicConsume("work", false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者2：" + new String(body));
                // 参数1：获取消息队列名称 参数2：是否一次消费多个消息
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }
}
